package com.xoomworks.rsfr.core.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;

import com.xoomworks.rsfr.core.service.RequirementService;
import com.xoomworks.rsfr.model.Requirement;

/**
 * @author marius.boeru
 */
@Service
public class RequirementServiceImpl extends BaseServiceImpl<Requirement> implements RequirementService{

    /**
     * Constructor.
     */
    public RequirementServiceImpl(){
        setClazz(Requirement.class);
        setEntityUri("Requirements");
        setClazzArray(Requirement[].class);
    }

    @Override
    public List<Requirement> getRequirementsByUserId() {
        HttpHeaders headers = new HttpHeaders();
        AuthenticationServiceImpl.setAuthenticationHeader(headers);
        HttpEntity<Requirement> requestEntity = new HttpEntity<Requirement>(null, headers);

        List<Requirement> reqList = new ArrayList<Requirement>();
        Requirement[] reqs = null;
        try {
            ResponseEntity<Requirement[]> returnedEntity = restTemplate.exchange(BASE_URL + "/"
                    + entityUri + "/Get/MyRequirements", HttpMethod.GET, requestEntity,
                    Requirement[].class);
            reqs = returnedEntity.getBody();
        } catch (RestClientException e) {
            e.printStackTrace();
        }

        for (Requirement res : reqs) {
            reqList.add(res);
        }
        return reqList;
    }

    @Override
    public List<Requirement> getAllRequirementsFromOwnCompany() {
        HttpHeaders headers = new HttpHeaders();
        AuthenticationServiceImpl.setAuthenticationHeader(headers);
        HttpEntity<Requirement> requestEntity = new HttpEntity<Requirement>(null, headers);

        List<Requirement> reqList = new ArrayList<Requirement>();
        Requirement[] reqs = null;
        try {
            ResponseEntity<Requirement[]> returnedEntity = restTemplate.exchange(BASE_URL + "/"
                    + entityUri + "/Get/RequirementsFromOwnCompany", HttpMethod.GET, requestEntity,
                    Requirement[].class);
            reqs = returnedEntity.getBody();
        } catch (RestClientException e) {
            e.printStackTrace();
        }

        for (Requirement res : reqs) {
            reqList.add(res);
        }
        return reqList;
    }


}
